/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2023. All rights reserved.
 * Description: UDK memory config defines
 * Author: -
 * Create: 2021.6.22
 */
#ifndef UDK_MCONFIG_H
#define UDK_MCONFIG_H

#include <stdint.h>
#include "udk_rwlock.h"
#include "udk_tailq.h"
#include "udk_fbarray.h"

#define UDK_MAX_TAILQ 32
#define UDK_DEFAULT_CHANNEL_NUM 4

struct udk_mem_config {
    uint32_t nchannel;    /* Number of channels (0 if unknown). */
    uint32_t nrank;       /* Number of ranks (0 if unknown). */
    uint32_t page_sz;
    uint32_t no_hugetlbfs; /* true to disable hugetblfs */

    udk_rwlock_t mlock;   /* used by memzones for thread safety. */
    udk_rwlock_t qlock;   /* used by tailqs for thread safety. */
    udk_rwlock_t mplock;  /* used by mempool library for thread safety. */

    struct udk_fbarray memzones; /* Memzone descriptors. */

    /* Tailqs for objects */
    struct udk_tailq_head tailq_head[UDK_MAX_TAILQ];

    /* udk_mem_config has to be mapped at the exact same address in all
     * processes, so we need to store it.
     */
    uint64_t mem_cfg_addr;  /* Address of this structure in memory. */

    uint8_t dma_maskbits;   /* Keeps the more restricted dma mask. */

    uint64_t tsc_hz; /* TSC rate */
};

void udk_mcfg_tailq_read_lock(void);
void udk_mcfg_tailq_read_unlock(void);
void udk_mcfg_tailq_write_lock(void);
void udk_mcfg_tailq_write_unlock(void);

void udk_mcfg_mempool_read_lock(void);
void udk_mcfg_mempool_read_unlock(void);
void udk_mcfg_mempool_write_lock(void);
void udk_mcfg_mempool_write_unlock(void);

uint32_t udk_memory_get_nchannel(void);
uint32_t udk_memory_get_nrank(void);

#endif /* UDK_MCONFIG_H */
